jquery 如何在symfony2中加载页面后在select中添加选项 您所在的位置:网站首页 easyui form 数据加载 jquery 如何在symfony2中加载页面后在select中添加选项

jquery 如何在symfony2中加载页面后在select中添加选项

2023-04-05 01:53| 来源: 网络整理| 查看: 265

我已经用这种方式使用ChoiceType字段。我认为这是合乎逻辑的选择。它的主要问题是(以及默认的EntityToIdTransformer或EntityChoiceList)是它水合每个可能的选项以便选择一个,这在某些情况下是多余的。你可能需要编写自己的转换器来防止这种情况。我使用 AJAX 在页面加载后将数据加载到选择中。它使页面更小,加快了页面处理时间,并使我能够更精确地缓存每组选项。这是针对Symfony 2.0的。它运行良好,我们在一个页面上放置了一堆Chosen字段,其中包含4000多个选项(尽管它只在用户与小部件交互时创建Chosen元素)。现在的限制是浏览器内存。

联系人实体类型

class ContactEntityType extends AbstractType { public function __construct(EntityManager $em) { $this->em = $em; } public function buildForm(FormBuilder $builder, array $options) { $repository = $this->em->getRepository('AcmeContactsBundle:Contact'); $builder->prependClientTransformer(new ContactToIdTransformer($repository)); } public function buildView(FormView $view, FormInterface $form) { $contact = $form->getData(); if($contact instanceof \Acme\ContactsBundle\Entity\Contact) { $view->set('choices', array($contact->getId() => $contact->getName())); } } public function getParent(array $options) { return 'choice'; } }

连接至变压器

这是内置EntityToIdTransformer的变体。

... class ContactToIdTransformer implements DataTransformerInterface { private $repository; public function __construct(EntityRepository $repository) { $this->repository = $repository; } public function transform($entity) { if (null === $entity || '' === $entity) { return null; } if (!is_object($entity)) { throw new UnexpectedTypeException($entity, 'object'); } if ($entity instanceof Collection) { throw new \InvalidArgumentException('Expected an object, but got a collection. Did you forget to pass "multiple=true" to an entity field?'); } return $entity->getId(); } public function reverseTransform($key) { if ('' === $key || null === $key) { return null; } if (!is_numeric($key)) { throw new UnexpectedTypeException($key, 'numeric'); } if (!($entity = $this->repository->find($key))) { throw new TransformationFailedException(sprintf('The entity with key "%s" could not be found', $key)); } return $entity; } }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有